/*
 * Project: BoboDesignPattern
 * 
 * File Created at 2020年2月25日
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package test.lesson3.sort;

import java.util.Arrays;

/**
 * @Type CountSort
 * @Desc
 * @author 52489
 * @date 2020年2月25日
 * @Version V1.0
 */
public class CountSort {
    public static void main(String[] args) {
        int[] nums = { 10, 6, 3, 8, 33, 27, 66, 9, 7, 88, 20 };
        System.out.println(Arrays.toString(count(nums)));
    }

    /**
     * @param nums
     * @return
     */
    private static int[] count(int[] nums) {
        int result[] = new int[nums.length];
        int max = nums[0];
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > max) {
                max = nums[i];
            }
        }
        int count[] = new int[max + 1];

        for (int i = 0; i < nums.length; i++) {
            count[nums[i]]++;
        }

        for (int i = 0, j = 0; i < count.length; i++) {
            while (count[i]-- > 0) {
                result[j++] = i;
            }
        }

        return result;

    }
}
